/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.netloader;

import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.jdpf.core.kernel.elements.Net;
import org.jdpf.core.kernel.logging.ILoggable;
import org.jdpf.core.kernel.logging.ILogger;
import org.jdpf.core.kernel.logging.LoggerFactory;
import org.jdpf.core.netloader.service.NetLoadingService;
import org.osgi.framework.BundleContext;
import org.xml.sax.SAXException;

/**
 * This class is a Service for the NetLoader Bundle.
 * <p>
 * It is responsible for activating the parser that reads an XML Net file and
 * build the related Net Object. This class is also the event handler of the Net
 * Parser events. It knows when the Sax parser has finished the parsing and a
 * Net object is returned invoking the event method <code>endParsing(Net)</code>.
 * </p>
 * 
 */

public class NetLoader implements NetLoadingService, ILoggable {
	
	private static final ILogger logger = LoggerFactory.getDefault().getLogger();
	private static final String NET_SCHEMA = "JDPFNet.xsd";
	
	private Boolean logging = true;
	
	private Net net;
	private BundleContext bc;
	private SAXParserFactory factory;

	

	public NetLoader(BundleContext bc) {
		net = null;
		this.bc = bc;

		factory = SAXParserFactory.newInstance();
		factory.setNamespaceAware(true);
		SchemaFactory schemaFactory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = null;
		try {
			URL f = (URL) bc.getBundle().findEntries("/", NET_SCHEMA, true).nextElement();
			schema = schemaFactory.newSchema(f);
			factory.setSchema(schema);
			factory.setValidating(true);
		} catch (Exception e) {
			logger.error(this, e.getMessage());
		}
	}

	public Net load(String file){
		net=null;
		try {
			if(logging) logger.logMainPoint(this, file, "Net loading started.");
			SAXParser parser = factory.newSAXParser();
			parser.parse(file, new NetEventHandler(this, file));
			if(logging) logger.logMainPoint(this, file, "Net loading completed.");
		} catch (ParserConfigurationException e) {
			logger.error(this, e.getMessage());
		} catch (SAXException e) {
		    logger.error(this, e.getMessage());
		} catch (IOException e) {
		    logger.error(this, e.getMessage());
		} catch (Exception e){
			logger.error(this,"Unable to load net "+file+" because of: "+e.toString());
		}
		return net;
	}

	public void endParsing(Net net) {
		this.net = net;
		if(net!=null&&net.isValid())
			if (logging) logger.log(this, net.getName());
	}


	public void enableLogging(Boolean logging) {
		this.logging = logging;
	}

}
